<?php

// +----------------------------------------------------------------------
// | ThinkAdmin
// +----------------------------------------------------------------------
// | 版权所有 2014~2020 广州楚才信息科技有限公司 [ http://www.cuci.cc ]
// +----------------------------------------------------------------------
// | 官方网站: http://demo.thinkadmin.top
// +----------------------------------------------------------------------
// | 开源协议 ( https://mit-license.org )
// +----------------------------------------------------------------------
// | gitee 代码仓库：https://gitee.com/zoujingli/ThinkAdmin
// | github 代码仓库：https://github.com/zoujingli/ThinkAdmin
// +----------------------------------------------------------------------

namespace app\index\controller;

use function PHPSTORM_META\type;
use think\admin\Controller;


/**
 * Class Index
 * @package app\index\controller
 */
class Register extends BaseController
{

    public $agreement_category_id = 31; //轮播图片
    public $table = "CmsUser";
    /**
     * 首页显示 3行内容
     */
    public function index()
    {
        if ($this->request->isGet()) {
            $this->title = '用户注册';
            $this->fetch();
        }else{
            $data = $this->request->post();
            $data['update_at'] = $data['create_at'] = time();
            if($this->app->db->name($this->table)->where(array("username"=>$data['username']))->find()){
                $this->error('注册失败，用户名已经被注册！');
            }

            if($this->app->db->name($this->table)->where(array("contact_phone"=>$data['contact_phone']))->find()){
                $this->error('注册失败，手机号已经被注册！');
            }

            $id = $this->app->db->name($this->table)->insertGetId($data);
            if ($id !== false) {
                $this->success('注册成功！快去登录吧');
            } else {
                $this->error('注册失败，请稍候再试！');
            }
        }
    }

    public function profile()
    {
        if ($this->request->isGet()) {
            $this->title = '用户资料';
            $this->fetch();
        }else{
            $data = $this->request->post();
            $data['update_at'] = time();

            if(0 && $data['code']!=session("code")){
                $this->error('验证码不正确');
            }

            unset($data['code']);
            $map = array("contact_phone"=>$data['contact_phone']);
            if(!$this->app->db->name($this->table)->where($map)->find()){
                $this->error('找回密码失败，手机号未被注册！');
            }

            $res = $this->app->db->name($this->table)->where($map)->update($data);
            if ($res > 0) {
                $this->success('修改密码成功！快去登录吧');
            } else {
                $this->error('修改密码失败，请稍候再试！');
            }
        }
    }

    public function updatepass(){
        if ($this->request->isGet()) {
            echo "无效的请求方式";
            exit();
        }

        $data = $this->request->post();
        if($data['password_new']!=$data['password_new2']){
            $this->error('新密码与新密码确认输入不一致');
        }

        $member = session("member");
        if($member==null){
            $this->error('会话已过期，请重新登录');
        }

        $map = array("id"=>$member['id'],"password"=>$data['password']);
        if(!$this->app->db->name($this->table)->where($map)->find()){
            $this->error('修改失败，原密码输入有误！');
        }

        $map = array("id"=>$member['id']);
        $toUpdate = [ "password"=>$data['password_new'], "update_at"=>time() ];
        $res = $this->app->db->name($this->table)->where($map)->update($toUpdate);
        if ($res > 0) {
            session("member", null);
            $this->success('修改密码成功！快去重新登录吧');
        } else {
            $this->error('修改密码失败，请稍候再试！');
        }
    }

    public function updateprofile(){
        if ($this->request->isGet()) {
            echo "无效的请求方式";
            exit();
        }

        $data = $this->request->post();
        $member = session("member");
        if($member==null){
            $this->error('会话已过期，请重新登录');
        }

        $map = array("id"=>$member['id']);
        $data['update_at'] = time();
        $res = $this->app->db->name($this->table)->where($map)->update($data);
        if ($res > 0) {
            //重新设置session的值
            session("member",$this->app->db->name($this->table)->where($map)->find());
            $this->success('资料修改成功！');
        } else {
            $this->error('资料修改失败，请稍候再试！');
        }
    }

    public function getpass()
    {
        if ($this->request->isGet()) {
            $this->title = '找回密码';
            $this->fetch();
        }else{
            $data = $this->request->post();
            $data['update_at'] = time();

            if(0 && $data['code']!=session("code")){
                $this->error('验证码不正确');
            }

            unset($data['code']);
            $map = array("contact_phone"=>$data['contact_phone']);
            if(!$this->app->db->name($this->table)->where($map)->find()){
                $this->error('找回密码失败，手机号未被注册！');
            }

            $res = $this->app->db->name($this->table)->where($map)->update($data);
            if ($res > 0) {
                $this->success('修改密码成功！快去重新登录吧');
            } else {
                $this->error('修改密码失败，请稍候再试！');
            }
        }
    }

    public function agreement()
    {
        $this->title = '使用协议';
        $list = $this->_get_category_articles($this->agreement_category_id, 0,1);
        $this->info = $list[0];
        $this->fetch();
    }


}